package com.test.messagewall.controller;

import com.test.messagewall.model.MessageInfo;
import com.test.messagewall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/messages")
public class MessageController {
    @Autowired
    private MessageService messageService;

    List<MessageInfo> list = new ArrayList<>();
    @RequestMapping("/publish")
    public boolean publish(MessageInfo messageInfo) {
        // 校验三个变量是否为空
        if (!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())) {
            return false;
        }

        // 不为空，将内容存入List
        messageService.publish(messageInfo);
        return true;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.getMessage();
    }
}
